﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace Skywave.IO
{
    public class CleanUp
    {

        private List<string> _Directories = new List<string>();
        /// <summary>
        /// ...
        /// </summary>
        public List<string> Directories
        {
            get { return _Directories; }
            set { _Directories = value; }
        }

        private List<string> _Exclusion_FileNames = new List<string>();
        /// <summary>
        /// ...
        /// </summary>
        public List<string> Exclusion_FileNames
        {
            get { return _Exclusion_FileNames; }
            set { _Exclusion_FileNames = value; }
        }

        Thread thread_CleanUp;
        public void Start()
        {
            thread_CleanUp = new Thread(new ThreadStart(Thread_CleanUp));
            thread_CleanUp.Start();
        }

        private TimeSpan cleanup_TimeSpan = new TimeSpan(1, 0, 0);
        public void Thread_CleanUp()
        {
            try
            {
                while (true)
                {
                    DateTime now = DateTime.UtcNow;
                    foreach (string fe1 in Directories)
                    {
                        CleanUpPath(fe1, now);
                    }
                    Thread.Sleep(new TimeSpan(1, 0, 0));
                }
            }
            catch (Exception)
            {
            }
        }

        private void CleanUpPath(string dirPath, DateTime now)
        {
            DirectoryInfo dir1 = new DirectoryInfo(dirPath);
            if (dir1.Exists)
            {
                foreach (FileInfo feFile1 in dir1.GetFiles())
                {
                    if ((from x1 in Exclusion_FileNames
                         where x1.ToLower() == feFile1.Name.ToLower()
                         select x1).Count() == 0 && (now - feFile1.LastWriteTimeUtc) > cleanup_TimeSpan)
                    {
                        try
                        {
                            feFile1.Delete();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                foreach (DirectoryInfo feDir1 in dir1.GetDirectories())
                {
                    CleanUpPath(feDir1.FullName, now);
                }
                if (dir1.GetFiles().Length == 0 && dir1.GetDirectories().Length == 0 && !Directories.Contains(dirPath))
                {
                    try
                    {
                        dir1.Delete(true);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        public void Stop()
        {
            if (thread_CleanUp != null)
                thread_CleanUp.Abort();
        }
    }
}
